Ext.define('SRMT.controller.Menu', {
	extend: 'Ext.app.Controller',
	
	views: [
	        'Menu'
	],
	
	refs: [{
		ref: 'btnLogin',
		selector: 'srmtMenu > #btnLogin'
	},{
		ref: 'btnRegister',
		selector: 'srmtMenu > #btnRegister'
	},{
		ref: 'btnLogout',
		selector: 'srmtMenu > #btnLogout'
	},{
		ref: 'btnAbout',
		selector: 'srmtMenu > button > #btnAbout'
	}],
	
	init: function() {
		this.control({
			'srmtMenu': {
				render: this.onRender
			},

			'srmtMenu > #btnLogin': {
				click: this.onBtnLoginClick
			},
			
			'srmtMenu > #btnLogout': {
				click: this.onBtnLogoutClick
			},
			
			'srmtMenu > #btnRegister': {
				click: this.onBtnRegisterClick
			},
			
			'srmtMenu > button > #btnAbout': {
				click: this.onBtnAboutClick
			}
		});
	},
	
	onRender: function() {
		
	},
	
	setMenuLogin: function() {
		this.getBtnLogin().setVisible(false);
		this.getBtnRegister().setVisible(false);
		this.getBtnLogout().setVisible(true);
	},
	
	setMenuLogout: function() {
		this.getBtnLogin().setVisible(true);
		this.getBtnRegister().setVisible(true);
		this.getBtnLogout().setVisible(false);
		Ext.Ajax.request({
			url: 'php/module/authenticate/logout.php'
		});
	},
	
	onBtnLoginClick: function(view, e, eOpts) {
		var viewportController = this.getController('SRMT.controller.Viewport');
		viewportController.setViewportDisable();
		var w = Ext.create('Ext.window.Window', {
			title: 'Login',
			width: 300,
			height: 140,
			resizable: false,
			items: [{
				xtype: 'srmtLogin'
			}]
		});
		w.show();
		w.on('destroy', function() {viewportController.onRender(); viewportController.setViewportEnable()}, this);
	},
	
	onBtnRegisterClick: function(view, e, eOpts) {
		var viewportController = this.getController('SRMT.controller.Viewport');
		viewportController.setViewportDisable();
		var w = Ext.create('Ext.window.Window', {
			title: 'Register',
			width: 300,
			height: 230,
			resizable: false,
			items: [{
				xtype: 'srmtRegister'
			}]
		});
		w.show();
		w.on('destroy', function() {viewportController.onRender(); viewportController.setViewportEnable()}, this);
	},
	
	onBtnLogoutClick: function(view, e, eOpts) {
		var viewportController = this.getController('SRMT.controller.Viewport');
		viewportController.setViewportDisable();
		Ext.MessageBox.show({
			title: 'Logout',
			msg: 'Are you sure to logout?',
			buttons: Ext.MessageBox.YESNO,
			icon: Ext.MessageBox.INFO,
			fn: function(btn) {
				if(btn == 'yes') {
					Ext.Ajax.request({
						url: 'php/module/authenticate/logout.php',
						async: false,
						timeout: 5000,
						success: function() {
							Ext.MessageBox.show({
								title: 'Logout',
								msg: 'You\'re logout!<br>Login to use.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.INFO
							});							
						},
						failure: function() {
							Ext.MessageBox.show({
								title: 'Logout',
								msg: 'Logout failed!',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
					});
					viewportController.onRender();
				}
				viewportController.setViewportEnable();
			}
		});

	},
	
	onBtnAboutClick: function(view, e, eOpts) {
		var tab = Ext.create('SRMT.view.About', {itemId: '0', closable: true});
		this.getController('SRMT.controller.Viewport').addTabToCenterPanel(tab);
	}
	
});